Ext.define('jwawa.erp.cfg.searchPage.SearchItemHandler', {
    extend: 'jwawa.erp.framework.ShellHandler',
    mixins: {
        searchItemHelper: 'jwawa.erp.cfg.searchPage.SearchItemHelper'
    },
    resources: [
        {
            name: 'searchItemModel',
            res: 'jwawa.erp.cfg.searchPage.SearchItemModel'
        },
        {
            name: 'entityDetailModel',
            res: 'jwawa.erp.cfg.entity.EntityDetailModel'
        },
        {
            name: 'entityDetailStore',
            type: 'store'
        }
    ],
    pageId: null,
    pageName: null,
    ref:{
        searchItemFormPanel: '#searchItemFormPanel',

        pageIdField: '#pageIdField',
        entityHeaderIdField: '#entityHeaderIdField',
        pageNameField: '#pageNameField',
        entityFieldLabel: '#entityFieldLabel',
        entityDetialIdCombo: '#entityDetialIdCombo',
        saveSearchItemButton: '#saveSearchItemButton',
        saveAndNewSearchItemButton: '#saveAndNewSearchItemButton',
        newSearchItemButton: '#newSearchItemButton',
        delSearchItemButton: '#delSearchItemButton',
        closeEditWinButton: '#closeEditWinButton'

    },
    bindHandler: function(){
        "use strict";
        var me = this,
            entityDetailModel = Ext.JwawaResMgr.get('entityDetailModel'),
            entityDetailStore = Ext.JwawaResMgr.get('entityDetailStore');

        me.entityDetialIdCombo.bindStore(entityDetailStore);

        me.initSearchEditWin();
        me.view.setPageState('initPage');
    },

    initSearchEditWin: function(){
        "use strict";
        var me = this;

//        me.searchItemFormPanel.getForm().url = '/search-page/save-search-item';
        me.saveSearchItemButton.on('click', me.onClickSaveSearchItemButton, me);
        me.saveAndNewSearchItemButton.on('click', me.onClickSaveAndNewSearchItemButton, me);
        me.newSearchItemButton.on('click', me.onClickNewSearchItemButton, me);
        me.delSearchItemButton.on('click', me.onClickDelSearchItemButton, me);
        me.closeEditWinButton.on('click', me.onCloseEditWinButton, me);

        me.view.addListener('afterRender', function(){
            fieldKeyNavBind(me.view, me.onClickSaveAndNewSearchItemButton, me);
        });

        me.entityDetialIdCombo.on('select', function(combo, records){
            var record = records[0];
            me.entityFieldLabel.setValue(record.get('entity_field_label'));
        });

        me.entityDetialIdCombo.on('change', function(combo, records){
            me.entityFieldLabel.setValue('');
        });
    },

    onContainerClose: function(ct){
        "use strict";
        var me = this;

        me.clearContent();
    },

    newSearchItem: function(pageId, pageName, entityHeaderId){
        "use strict";
        var me = this,
            form = me.searchItemFormPanel.getForm(),
            entityDetailModel = Ext.JwawaResMgr.get('entityDetailModel');

        me.clearContent();

        me.view.setPageState('newPage');
        form.setValues({
            page_id: pageId,
            page_name: pageName,
            entity_header_id: entityHeaderId
        });
        me.entityDetialIdCombo.focus(true, DEFAULT_FOCUS_DEFER_TIME);
    },

    editSearchItem: function(searchItem, pageId, pageName, entityHeaderId){
        "use strict";
        var me = this,
            form = me.searchItemFormPanel.getForm(),
            entityDetailModel = Ext.JwawaResMgr.get('entityDetailModel');

        me.clearContent();

        me.view.setPageState('editPage');
        Ext.apply(searchItem, {
            page_id: pageId,
            page_name: pageName,
            entity_header_id: entityHeaderId
        });
        form.setValues(searchItem);
        me.entityDetialIdCombo.focus(true, DEFAULT_FOCUS_DEFER_TIME);
    },

    onClickSaveSearchItemButton: function(){
        "use strict";
        var me = this,
            searchItemModel = Ext.JwawaResMgr.get('searchItemModel');

        var form = me.searchItemFormPanel.getForm();

        if (form.isValid()) {
            me.view.setLoading(true);
            searchItemModel.saveSearchItem(form.getFieldValues(),{
                success:function(searchItem){
                    me.afterSaveSearchItem(searchItem);
                    me.editSearchItem(searchItem,
                        me.pageIdField.getValue(),
                        me.pageNameField.getValue(),
                        me.entityHeaderIdField.getValue());
                },
                callback: function(){
                    me.view.setLoading(false);
                }
            });
        }
    },

    afterSaveSearchItem: function(searchItem){
        "use strict";
        var me = this,
            searchItemStore = Ext.JwawaResMgr.get('searchItemStore'),
            searchItemRecord = searchItemStore.getById(searchItem['id']);

        if(!Ext.isEmpty(searchItemRecord)){
            searchItemRecord.set('entity_field_name', searchItem['entity_field_name']);
            searchItemRecord.set('entity_field_label', searchItem['entity_field_label']);
            searchItemRecord.set('item_type', searchItem['item_type']);
            searchItemRecord.set('item_class', searchItem['item_class']);
            searchItemRecord.set('seq', searchItem['seq']);
            searchItemRecord.set('condition_flag', searchItem['condition_flag']);
            searchItemRecord.set('item_cfg', searchItem['item_cfg']);

            searchItemRecord.commit();
        }else{
            searchItemStore.add(searchItem);
        }

        top.msgBar.showSuccessMsg('检索页明细保存成功');
    },

    onClickSaveAndNewSearchItemButton: function(){
        "use strict";
        var me = this,
            searchItemModel = Ext.JwawaResMgr.get('searchItemModel'),
                searchItemStore = Ext.JwawaResMgr.get('searchItemStore');

        var form = me.searchItemFormPanel.getForm();

        if (form.isValid()) {
            me.view.setLoading(true);
            searchItemModel.saveSearchItem(form.getFieldValues(),{
                success:function(searchItem){
                    me.afterSaveSearchItem(searchItem);
                    me.newSearchItem(me.pageIdField.getValue(),
                        me.pageNameField.getValue(),
                        me.entityHeaderIdField.getValue());
                },
                callback: function(){
                    me.view.setLoading(false);
                }
            });
        }
    },

    onClickNewSearchItemButton: function(){
        "use strict";
        var me = this;

        me.newSearchItem(me.pageIdField.getValue(),
            me.pageNameField.getValue(),
            me.entityHeaderIdField.getValue());
    },

    onClickDelSearchItemButton: function(){
        "use strict";
        var me = this,
            searchItemModel = Ext.JwawaResMgr.get('searchItemModel'),
            searchItemStore = Ext.JwawaResMgr.get('searchItemStore'),
            form = me.searchItemFormPanel.getForm(),
            itemId = parseInt(form.findField('id').getValue());

        alertConfirm('提示','真的要执行删除操作吗？', function(buttonId){
            if(buttonId == 'yes'){
                me.view.setLoading(true);
                searchItemModel.delSearchItem(itemId, function(){
                    me.afterDelSerchItem(itemId);
                    me.view.setLoading(false);
                    me.view.ownerCt.close();
                });
            }
        });
    },

    onCloseEditWinButton: function(){
        "use strict";
        var me = this;

        me.view.ownerCt.close();
    },

    clearContent: function(){
        "use strict";
        var me = this;

        me.searchItemFormPanel.getForm().reset();
    },

    onPageStateChange: function(obj, newState, oldState){
        "use strict";
        var me = this;

        switch(newState){
            case 'delPage':
            case 'initPage':
                me.saveSearchItemButton.disable();
                me.saveAndNewSearchItemButton.disable();
                me.newSearchItemButton.disable();
                me.delSearchItemButton.disable();
                me.closeEditWinButton.enable();
                break;
            case 'newPage':
                me.saveSearchItemButton.enable();
                me.saveAndNewSearchItemButton.enable();
                me.newSearchItemButton.enable();
                me.delSearchItemButton.disable();
                me.closeEditWinButton.enable();
                break;
            case 'editPage':
                me.saveSearchItemButton.enable();
                me.saveAndNewSearchItemButton.enable();
                me.newSearchItemButton.enable();
                me.delSearchItemButton.enable();
                me.closeEditWinButton.enable();
                break;
        }
    }
});